package yanyan.com.tochar.utils;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


import java.io.IOException;

import pl.droidsonroids.gif.GifDrawable;
import yanyan.com.tochar.R;

public class ImageDialog extends Dialog {
    private TextView title;
    private Button left, right;
    private ImageView imageView;


    private LeftOnlistener leftFun;
    private RightOnlistener rightFun;
    private String titleStr;
    private String leftStr, rightStr;
    private Bitmap image;
    private boolean isGif;
    private String gifPath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_image);
        title = findViewById(R.id.dialog_image_title);
        left = findViewById(R.id.dialog_image_leftbt);
        right = findViewById(R.id.dialog_image_rightbt);
        imageView = findViewById(R.id.dialog_image_pic);
        initEvent();
        initDate();
    }


    public ImageDialog(Context context,
                       String titleStr, String leftStr, String rightStr,
                       Bitmap image,
                       LeftOnlistener leftFun, RightOnlistener rightFun) {
        super(context);
        this.leftFun = leftFun;
        this.rightFun = rightFun;
        this.titleStr = titleStr;
        this.leftStr = leftStr;
        this.rightStr = rightStr;
        this.image = image;
    }

    public ImageDialog(Context context,
                       String titleStr, String leftStr, String rightStr,
                       Bitmap image,
                        boolean isGif, String gifPath) {
        super(context);
        this.leftFun = leftFun;
        this.rightFun = rightFun;
        this.titleStr = titleStr;
        this.leftStr = leftStr;
        this.rightStr = rightStr;
        this.image = image;
        this.isGif = isGif;
        this.gifPath = gifPath;

    }


    public ImageDialog(Context context,
                       String titleStr, String leftStr, String rightStr,
                       Bitmap image) {
        super(context);
        this.titleStr = titleStr;
        this.leftStr = leftStr;
        this.rightStr = rightStr;
        this.image = image;
    }

    private void initDate() {
        if (EmptyUtils.isNotEmpty(titleStr)) {
            title.setText(titleStr);
        }
        if (isGif) {
            GifDrawable gifDrawable= null;
            try {
                gifDrawable = new GifDrawable(gifPath);
                imageView.setImageDrawable(gifDrawable);
            } catch (IOException e) {
                e.printStackTrace();
            }

        } else {
            if (image != null) {
                imageView.setImageBitmap(image);
            }
        }


        if (EmptyUtils.isNotEmpty(leftStr)) {
            left.setText(leftStr);
        }

        if (EmptyUtils.isNotEmpty(rightStr)) {
            right.setText(rightStr);
        }

        if (leftFun == null) {
            leftFun = new LeftOnlistener() {
                @Override
                public void leftOnclick() {

                }
            };
        }


        if (rightFun == null) {
            rightFun = new RightOnlistener() {
                @Override
                public void rightOnclick() {

                }
            };
        }
    }

    public void setLeftOnclick(LeftOnlistener leftFun) {
        this.leftFun = leftFun;
    }

    ;

    public void setRightOnclick(RightOnlistener rightFun) {
        this.rightFun = rightFun;
    }

    ;

    /**
     * 初始化方法
     */
    private void initEvent() {
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                leftFun.leftOnclick();
                dismiss();
            }
        });

        right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rightFun.rightOnclick();
                dismiss();
            }
        });
    }

    public interface LeftOnlistener {
        public void leftOnclick();
    }

    public interface RightOnlistener {
        public void rightOnclick();
    }

    public ImageDialog(Context context) {
        super(context);
    }


}
